package com.lp.controller;

import com.lp.userException.CustomException;
import com.lp.po.PagingVO;
import com.lp.po.SceneryList;
import com.lp.service.SceneryListService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/SceneryList")
public class AdminSceneryListController {
    @Resource
    private SceneryListService sceneryListService;
    /***************************景点列表管理设置****************************************/
    @RequestMapping("/showSceneryList")
    public String showSceneryList(Model model, Integer page) throws Exception {
        List<SceneryList> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int SceneryListNumber=sceneryListService.getSceneryListCount();
        //设置总页数
        pagingVO.setTotalCount(this.sceneryListService.getSceneryListCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = sceneryListService.findSceneryList(1);
        } else {
            pagingVO.setToPageNo(page);
            list = sceneryListService.findSceneryList(page);
        }
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("scenerynameList",list);
        model.addAttribute("number", SceneryListNumber);
        return "/admin/showSceneryList";
    }
    @RequestMapping(value = "/editSceneryList",method = RequestMethod.GET)
    public String editSceneryList(Model model,Integer sceneryid) throws Exception {
        if (sceneryid == null) {
            return "redirect:/admin/SceneryList/showScenery";
        }
        SceneryList sceneryList = sceneryListService.findById(sceneryid);
        if (sceneryList == null) {
            throw new CustomException("没有该景点");
        }
        model.addAttribute("sceneryList", sceneryList);
        return "/admin/editSceneryList";
    }
    @RequestMapping(value = "/doEditSceneryList",method = RequestMethod.POST)
    public String doEditSceneryList(SceneryList sceneryList,Integer sceneryid,String sceneryname,MultipartFile file) throws Exception {
        sceneryListService.updataSceneryname(sceneryid,sceneryname,sceneryList.getIconPath());
        return "redirect:/admin/SceneryList/showSceneryList";
    }
    @RequestMapping(value = "/removeSceneryList")
    public String removeSceneryList(Integer sceneryid) throws Exception {
        sceneryListService.delectSceneryList(sceneryid);
        return "redirect:/admin/SceneryList/showSceneryList";
    }
    @RequestMapping(value = "/addSceneryList")
    public String addSceneryList() throws Exception {
        return "admin/addSceneryList";
    }
    @RequestMapping(value = "/doaddSceneryList")
    public String doaddSceneryList(Model model,SceneryList sceneryList,MultipartFile file) throws Exception {
        Boolean result = sceneryListService.save(sceneryList);
        if (!result) {
            //重复就回到添加页，添加成功后回到展示页
            model.addAttribute("message","请输入正确的数据！");
            return "forward:/admin/SceneryList/addSceneryList";
        }
        //重定向
        return "redirect:/admin/SceneryList/showSceneryList";
    }
}
